package com.csi.okr.mobile.common.task;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.csi.okr.mobile.common.dao.VOkrOrgDao;
import com.csi.okr.mobile.common.entity.VOkrOrg;

/**
 * 缓存定时任务
 */
@Component
public class DataTask {
	
	@Resource
	private VOkrOrgDao dao;
	
	
	/**
	 * 公司组织结构
	 */
	public static List<VOkrOrg> ORG_STRUCTURE;
	/**
	 * O色值
	 */
	public static List<Map<String, Object>> OCOLOR = new ArrayList<Map<String, Object>>();
	/**
	 * KR色值
	 */
	public static List<Map<String, Object>> KRCOLOR = new ArrayList<Map<String, Object>>();
	/**
	 * O状态码
	 */
	public static List<Map<String, Object>> O_STATE = new ArrayList<Map<String, Object>>();
	/**
	 * 所属团队
	 */
	public static List<Map<String, Object>> O_TEAM = new ArrayList<Map<String, Object>>();
	/**
	 * REVIEW状态码
	 */
	public static List<Map<String, Object>> R_STATE = new ArrayList<Map<String, Object>>();
	/**
	 * 评分与总结状态
	 */
	public static List<Map<String, Object>> S_STATE = new ArrayList<Map<String, Object>>();
	
	//项目运行时加载
	@PostConstruct
	public void init(){
		doOrgStructure();
		initData();
	}
	
	//每天凌晨执行 - 定时任务
	@Scheduled(cron = "0 0 0 * * ? ")  
	public void execute(){
		initData();
		doOrgStructure();
	}
	
	//加载组织结构
	public void doOrgStructure(){
		String hql = "from VOkrOrg where orgParentUuid = :orgParentUuid order by orgLevel asc,orgSeqNum asc";
		Map<String, Object> map = new HashMap<String, Object>();
		//中软国际UUID
		map.put("orgParentUuid", "1e678e8b-10189f46324-fbae402534d30a12ebdd8049c56747d3");
		//查询中软国际下级组织
		List<VOkrOrg> topOrg = dao.findList(hql, map);
		//存入静态变量
		ORG_STRUCTURE = topOrg;
	}
	
	//初始化数据 
	public void initData(){
		//O分数颜色
		String sql_o = "SELECT * FROM T_OKR_LIMITSWITCH_VALUE WHERE LIMITSWITCH_CATEGORY_CODE = 'CL' AND LIMITSWITCH_CODE = 'CO'";
		OCOLOR = dao.excuteSql(sql_o);
		//KR分数颜色
		String sql_kr = "SELECT * FROM T_OKR_LIMITSWITCH_VALUE WHERE LIMITSWITCH_CATEGORY_CODE = 'CL' AND LIMITSWITCH_CODE = 'CK'";
		KRCOLOR = dao.excuteSql(sql_kr);
		//O状态
		String sql_os = "SELECT * from T_OKR_BASECODE_CODE WHERE BASECODE = 'OS' ORDER BY SORT_ID";
		O_STATE = dao.excuteSql(sql_os);
		//O所属团队
		String sql_ot = "SELECT * from T_OKR_BASECODE_CODE WHERE BASECODE = 'OT'";
		O_TEAM = dao.excuteSql(sql_ot);
		//Review状态
		String sql_rs = "SELECT * from T_OKR_BASECODE_CODE WHERE BASECODE = 'RS'";
		R_STATE = dao.excuteSql(sql_rs);
		//评分状态
		String sql_ss = "SELECT * from T_OKR_BASECODE_CODE WHERE BASECODE = 'SS'";
		S_STATE = dao.excuteSql(sql_ss);
	}
}
